package com.ypf.demo_ssm.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.listener.PageReadListener;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.fastjson.JSON;
import com.ypf.demo_ssm.dto.request.UserDTO;
import com.ypf.demo_ssm.dto.response.UserOrdersVO;
import com.ypf.demo_ssm.entity.User;
import com.ypf.demo_ssm.service.impl.UserServiceImpl;
import com.ypf.demo_ssm.utils.LocalDateConverter;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 殷鹏飞
 * @since 2024-06-15
 */
@RestController
@RequestMapping("/user")
@Tag(name = "用户")
public class UserController {

    @Resource
    private UserServiceImpl userService;

    @Operation(summary = "模糊查询用户列表")
    @GetMapping("/queryList")
    public List<User> queryList(@RequestParam(value = "username", required = false) String username,
                                @RequestParam(value = "account", required = false) String account) {
        return userService.queryList(username, account);
    }

    @Operation(summary = "根据id获取用户信息")
    @GetMapping("/getById")
    public User getById(@RequestParam("id") String id) {
        return userService.getById(id);
    }

    @Operation(summary = "根据id删除")
    @DeleteMapping("/deleteById")
    public Boolean deleteById(@RequestParam("id") String id) {
        return userService.removeById(id);
    }

    @Operation(summary = "新增或更新用户信息")
    @PostMapping("addOrUpdate")
    public Boolean addOrUpdate(@RequestBody User user) {
        return userService.addOrUpdate(user);
    }

    @Operation(summary = "导出excel")
    @GetMapping("/exportExcel")
    public void exportExcel(HttpServletResponse response) {
        try {
            // 设置响应结果
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setCharacterEncoding("utf-8");
            String fileName = URLEncoder
                    .encode("用户信息表", StandardCharsets.UTF_8)
                    .replaceAll("\\+", "%20");
            response.setHeader("Content-disposition", "attachment;filename*=" + fileName + ".xlsx");
            // 导出
            userService.exportExcel(response.getOutputStream());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Operation(summary = "导入excel")
    @PostMapping("/importExcel")
    public void importExcel(@RequestParam("file") MultipartFile file) {
        try {
            userService.importExcel(file);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Operation(summary = "查询用户及其订单列表")
    @PostMapping("/queryUserOrders")
    public List<UserOrdersVO> queryUserOrders(@RequestBody UserDTO userDTO) {
        return userService.selectUserAndOrders(userDTO);
    }


}
